package com.amigo.online.provider.sync.handle.service.impl;

import java.util.Map;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.entity.VideoMysqlInfo;
import com.amigo.online.provider.sync.abs.BasicMongoDataProjectProperties;
import com.amigo.online.provider.sync.convert.ConvertJsonObjectService;
import com.amigo.online.provider.sync.handle.data.mongo.service.VideoMongoService;
import com.amigo.online.provider.sync.handle.service.AbstractDataHandler;
import com.amigo.online.provider.sync.handle.service.DataHandler;

@Service("videoMongoDataHandler")
public class VideoMongoDataHandler extends AbstractDataHandler implements DataHandler {

    @Autowired
    VideoMongoService videoMongoService;

    @Autowired
    BasicMongoDataProjectProperties basicMongoDataProjectProperties;

    @Autowired
    @Qualifier("mongoVideoConvertServiceImpl")
    ConvertJsonObjectService<Document, JSONObject, VideoMysqlInfo> mongoVideoconvertJsonObjectService;

    @Override
    public void updateHandle(JSONObject newObject, JSONObject oldObjecet) {
        int delete = newObject.getIntValue("deleted");
        long id = newObject.getLongValue("id");
        Query query = new Query().addCriteria(Criteria.where("_id").is(id));
        long result = 0;
        Map<String, String> projectMap = basicMongoDataProjectProperties.getVideoProjectMap();
        if (delete == 0) {
            Update update = new Update();
            boolean updateFlag = false;
            for (String temp : oldObjecet.keySet()) {
                if (basicMongoDataProjectProperties.getVideoUpdateList().contains(temp)) {
                    String projectColumn = projectMap.get(temp);
                    if (projectColumn != null) {
                        update.set(projectColumn, newObject.get(temp));
                    } else {
                        update.set(temp, newObject.get(temp));
                    }
                    updateFlag = true;
                }
            }
            if (updateFlag == true) {
                result = videoMongoService.updateObject(query, update);
                if (result == 0) {
                    logger.error("");
                    //kafkaMessageSender.failLogSend("", "");
                }
            }
        } else if (delete == 1) {
            result = videoMongoService.deleteObject(query);
            if (result != 1) {
                logger.error("");
                //kafkaMessageSender.failLogSend("", "");
            }
        }

    }

    @Override
    public void insertHandle(JSONObject newObject) {
        Document videoDoc = mongoVideoconvertJsonObjectService.convert(newObject);
        videoMongoService.insertObject(videoDoc);
    }

    @Override
    public void updateInfoByIdThroughHashMap(Long id, Map<String, Object> updateMap) {

    }

    @Override
    public void insertObject(Object source) {
        Document videoDoc = mongoVideoconvertJsonObjectService.convertBeanToObject((VideoMysqlInfo) source);
        videoMongoService.insertObject(videoDoc);
    }

}
